<%@ page pageEncoding="UTF-8"%>
<%@ include file="/common/common.jsp"%>
<%!
public Map getMap(List<Map> result , Map<String,String> deptMap){
	
		//处理结合数据
		for(Map map : result){
		
			String ksdm = map.get("ksdm") == null ? "0" : map.get("ksdm").toString();
			map.put("name",deptMap.get(ksdm));
			
		}
		
		Map<String,Object> resMap = new HashMap<String,Object>();
		resMap.put("rank",result);
		
		return resMap;
	}
%>
<%
	//查询住院收入--科室排名
	String yydm =  input.getDesc("yydm","02");//医院代码	
	String date = input.getDesc("start_date","2017-12-18");//开始时间
	String endDa = input.getDesc("end_date","2017-12-24");//结束时间

	SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
	SimpleDateFormat sdf2 = new SimpleDateFormat("yyyyMMdd");
	String nowDate = sdf2.format(sdf.parse(date));
	String nowEndDa = sdf2.format(sdf.parse(endDa));
	
	String startDate = nowDate+"00:00:00";
	String endDate = nowEndDa+"23:59:59";

	String sqlWhere = " and js.zxrq > '"+ startDate + "' and js.zxrq < '" + endDate+"' " ;
	if(!yydm.equals("-1"))
	sqlWhere += " and js.ksdm in (select new map (id as id) from his_dept where yjks_id != 1010 and yydm ='"+yydm+"') " ;
	
	
	//获取所有科室
	List<Map> deptList = QBuilder.db("basedb").queryHql("select new map(id as id , name as name ) from his_dept ");
	Map<String,String> deptMap = new HashMap<String,String>();
	//科室放到map中 key-value(id-name)
	for(Map map : deptList){
		deptMap.put(map.get("id").toString(),map.get("name").toString());
	}
	
	//住院收入

	List<Map> feelist = QBuilder.db("basedb").queryHql("select new map( js.ksdm as ksdm , sum(js.zje) as zje  ) from ZY_BRFYMXK js  where js.jlzt = 0 " + sqlWhere+ " group by ksdm order by sum(js.zje) desc");

	DecimalFormat df = new DecimalFormat("######0.0000"); 
	for(Map map : feelist ){
		double moneyTemp = Double.parseDouble(map.get("zje") ==null ? "0" : map.get("zje").toString());

		map.put("xmje",df.format(moneyTemp/10000));
	}
	
	
	Map<String,Object> ma1 = new HashMap<String,Object>();
	ma1.put("ksMap",getMap(feelist,deptMap));

	Message msg = new Message();

	msg.put("errcode",0);
	msg.put("zy_charge_dept",ma1);

	out.println(msg);
%>